package com.accounting.client.frontend.order;

import com.accounting.client.frontend.common.MoneyField;
import com.accounting.client.remote.enumeration.Currency;
import com.accounting.client.remote.enumeration.StockLocation;
import com.accounting.client.remote.obj.OrderItem;
import com.accounting.client.resource.BoxResources;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.form.ImageWidget;
import corelib.client.ui.widget.form.IntComboWidget;
import corelib.client.ui.widget.form.SelectField;
import corelib.client.ui.widget.form.SimpleLabel;
import corelib.client.ui.widget.form.fomatters.ValueFormatter;
import corelib.client.ui.widget.layout.LabelAlignment;
import corelib.client.ui.widget.layout.RelativeLayout;
import corelib.client.ui.widget.layout.RelativeRow;

public class OrderItemRow extends RelativeRow {
	
	private OrderItem   orderItem;
	private MoneyField  costField;
	private MoneyField  priceField;
	private MoneyField  postageField;
	private IntComboWidget countField;
	private OrderItemsPanel panel;
	private SelectField<StockLocation> locationField;
	
	public OrderItemRow(OrderItem item, OrderItemsPanel column) {
		this.panel = column;
		this.orderItem  = item;
		Init();
	}

	protected void Init(){
		int lw = 100;
		int fw = 100;
		
		add(new SimpleLabel(" ").setBold(true), new SimpleLabel(orderItem.getProductName()).setBold(true),
				RelativeLayout.create(200, LabelAlignment.Top, lw)
		);
		
		costField  = new MoneyField(Currency.AUD);
		priceField = new MoneyField(Currency.RMB);
		priceField.getValueField().setEnabled(true);
		
		postageField  = new MoneyField(Currency.AUD);
		locationField = new SelectField<StockLocation>(StockLocation.All);
		locationField.setNullable(true);
		locationField.setEnabled(false);
		locationField.setOptionFormatter("库存位置",new ValueFormatter<StockLocation>(){
			@Override
			public String format(StockLocation value) {
				return value.getCname();
			}
		});
		
		costField.getValueField().setEnabled(false);
		postageField.getValueField().setEnabled(false);
		
		RelativeLayout layout = RelativeLayout.create(fw, LabelAlignment.Top, lw);
		add(new SimpleLabel("数量").setBold(true),   countField = new IntComboWidget(1, this.orderItem.getCount()),layout);
		addSpace(12);
		add(new SimpleLabel("购买成本").setBold(true), costField.getValueField(),layout);
		addSpace(12);
		add(new SimpleLabel("库存邮费").setBold(true), postageField.getValueField(),layout);
		addSpace(12);
		add(new SimpleLabel("库存位置").setBold(true), locationField,layout);
		addSpace(12);
		add(new SimpleLabel("销售单价").setBold(true),  priceField.getValueField(), layout);
		addSpace(12);
		
		ImageWidget closeIcon = new ImageWidget(BoxResources.INSTANCE.cancel16(), new Size(16, 16), new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				panel.removeOrderRow(OrderItemRow.this);
			}
		});
		
		closeIcon.getElement().getStyle().setMarginTop(25, Unit.PX);
		add(closeIcon);
		bind();
	}
	
	private void bind() {
		if (orderItem != null) {
			countField.setInteger(orderItem.getCount());
			costField.getValueField().setDouble(orderItem.getCost(),false);
			postageField.getValueField().setDouble(orderItem.getPostage(), false);
			locationField.setSelectedOption(StockLocation.getLocationByType(orderItem.getLocation()));
		}
	}
	
	public boolean validate() {
		if (countField.getInteger() == null || countField.getInteger() == 0.0){
			countField.markError("请选择销售数量.");
			return false;
		}
		
		if (priceField.getValueField().getDouble() == null || priceField.getValue() == 0.0){
			priceField.getValueField().markError("请输入销售单价，销售价格不能为零");
			return false;
		}
		return true;
	}
	
	public OrderItem getData() {
		OrderItem item = new OrderItem();
		item.setStockId(orderItem.getStockId());
		item.setLocation(orderItem.getLocation());
		item.setProductId(orderItem.getProductId());
		item.setProductName(orderItem.getProductName());
		item.setCost(orderItem.getCost());
		item.setPostage(orderItem.getPostage());
		item.setPriceCurrency(priceField.getCurrencyField().getSelectedOption().getName());
		item.setCostCurrency(costField.getCurrencyField().getSelectedOption().getName());
		item.setPostageCurrency(postageField.getCurrencyField().getSelectedOption().getName());
		
		item.setPrice(priceField.getValue());
		item.setCount(countField.getInteger());
		return item;
	}
	
}
